<?php
if (! defined('LINKS_ADMIN_AREA')) {
	define('LINKS_ADMIN_AREA', 1);
}

$mytrustdirpath = dirname(__FILE__);


require XOOPS_ROOT_PATH.'/include/cp_functions.php' ;

if (!isset($xoopsModule) || !is_object($xoopsModule)) {
	$module_handler =& xoops_gethandler('module');
	$xoopsModule =& $module_handler->getByDirname($mydirname);
}

// ALTSYS
$mydirpath = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
if (! empty($_GET['lib'])) {
	$lib = preg_replace('/[^a-zA-Z0-9_-]/', '', $_GET['lib']);
	$page = preg_replace('/[^a-zA-Z0-9_-]/', '', @$_GET['page']);

	// language file (for X2 tpladmin)
	$langmanpath = XOOPS_TRUST_PATH.'/libs/altsys/class/D3LanguageManager.class.php';
	if (!file_exists($langmanpath)) die('install the latest altsys');
	require_once( $langmanpath );
	$langman =& D3LanguageManager::getInstance();
	$langman->read('modinfo.php', $mydirname, $mytrustdirname);

	require_once $mytrustdirpath .'/include/functions.php';
	if (!defined('XOOPS_CUBE_LEGACY') && $page == 'mytplsadmin') {
		// X2 and mytplsadmin
		include $mytrustdirpath .'/include/mytplsadmin.php';
	} else {
		// XCL and X2others
		if ( file_exists( XOOPS_TRUST_PATH .'/libs/'.$lib.'/'.$page.'.php' ) ) {
			include XOOPS_TRUST_PATH .'/libs/'.$lib.'/'.$page.'.php' ;
		} else if ( file_exists( XOOPS_TRUST_PATH .'/libs/'.$lib.'/index.php' ) ) {
			include XOOPS_TRUST_PATH .'/libs/'.$lib.'/index.php' ;
		} else {
			die('wrong request');
		}
	}
	exit ;
}


require_once $mytrustdirpath .'/class/links_Base.class.php';

$class_file = !empty($_GET['c']) ? preg_replace('/[^a-zA-Z0-9_-]/', '', $_GET['c']): 'index';
$method     = !empty($_GET['m']) ? preg_replace('/[^a-zA-Z0-9_-]/', '', $_GET['m']): 'index';

//Controller File Load
if (! file_exists($mytrustdirpath .'/admin/'. $class_file .'.php')) {
	redirect_header(XOOPS_URL .'/user.php', 2, 'No File');//TODO
	exit();
}
require_once $mytrustdirpath .'/admin/'. $class_file .'.php';


//Controller Instance
$class_name = 'links_admin_'. $class_file;
if ( !class_exists($class_name) || strncmp($method, '_', 1) == 0 || !in_array($method, get_class_methods($class_name)) ) {
	redirect_header(XOOPS_URL .'/user.php', 2, 'No Method');//TODO
	exit();
}
$Links_admin_controller = new $class_name($mydirname);

//EXEC
$args = get_arguments($class_file, $method);
call_user_func_array(array(&$Links_admin_controller, $method), $args);


/*---------------------------------------------*/
function get_arguments($class_file, $method)
{
	$args = array();

	switch ($class_file) {
		case 'categories':
			if ($method == 'index' || $method == 'check') {
				$args[0] = isset($_GET['cid']) ? intval($_GET['cid']): 0;
				break;
			}
		case 'broken':
			if ($method == 'index') {
				$args[0] = isset($_GET['s']) ? intval($_GET['s']): 0;
				$args[1] = isset($_GET['p']) ? intval($_GET['p']): 0;
				break;
			}
			if ($method == 'change_status') {
				$args[0] = isset($_GET['reportid']) ? intval($_GET['reportid']): 0;
				$args[1] = isset($_GET['s']) ? intval($_GET['s']): 0;
				break;
			}
		case 'request':
			if ($method == 'index') {
				$args[0] = isset($_GET['s']) ? intval($_GET['s']): 0;
				$args[1] = isset($_GET['p']) ? intval($_GET['p']): 0;
				break;
			}
			if ($method == 'change_status') {
				$args[0] = isset($_GET['requestid']) ? intval($_GET['requestid']): 0;
				$args[1] = isset($_GET['s']) ? intval($_GET['s']): 0;
				break;
			}
		case 'approval':
			if ($method == 'index') {
				$args[0] = isset($_GET['s']) ? intval($_GET['s']): 0;
				$args[1] = isset($_GET['p']) ? intval($_GET['p']): 0;
				break;
			}
		default:
			break;
	}

	return $args;
}
